package com.wx.mall.demo.controller;


import com.alibaba.fastjson.JSON;
import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.demo.dto.DemoDto;
import com.wx.mall.demo.entity.Demo;
import com.wx.mall.demo.service.IDemoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-03-23
 */
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    IDemoService iDemoService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public CommonResult<List<Demo>> list(@Validated DemoDto demoDto){
        System.out.println(JSON.toJSON(demoDto.toString()));
        List<Demo> list = iDemoService.list();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "列表-分页")
    @GetMapping("/list/page")
    public CommonResult<CommonPage<Demo>> list(Integer pageNum,Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
        List<Demo> list = iDemoService.list(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(list)); // 返回分页查询结果
    }

}

